#!/usr/bin/env bash

JDK_VERSION=110
JDK_PKG=openjdk-11-jdk

PROJECT_PATH=.

PATH_PROFILE=/etc/profile
ANDROID_HOME=$HOME/Android/Sdk
ANDROID_HOME_CACHE=$ANDROID_HOME/.cache

# Get JDK Version
javaVersion() {
    echo $(java -version 2>&1 | sed -n ';s/.* version "\(.*\)\.\(.*\)\..*".*/\1\2/p;')
}

# Check and install package with APT
checkAndInstall() {
    echo "Checking $1 ..."
    if [ "$(command -v $1)" = "" ]; then
        echo "Installing $1 now!"
        apt -qq install -y $1
        echo "$1 installed!"
    fi
}

initEnv() {
    echo "Init environment"

    if [ ! -f $PATH_PROFILE ]; then
        echo "Path profile '$PATH_PROFILE' not found!"
        exit 1
    fi

    echo "Updating APT index ..."
    apt -qq update

    checkAndInstall curl
    checkAndInstall wget
    checkAndInstall unzip
}

checkJDK() {
    echo "Checking JDK ..."
    if [ "$(javaVersion)" != "$JDK_VERSION" ]; then
        echo "Installing $JDK_PKG now!"
        apt -qq install -y $JDK_PKG
        echo "$JDK_PKG installed!"
    fi
}

checkAndroidCmdLineTools() {
    echo "Checking Android command line tools ..."
    if [ "$(command -v sdkmanager)" = "" ]; then
        echo "Installing Android command line tools now!"
        DOWNLOAD_LINK=$(curl -s "https://developer.android.com/studio" | grep -Eo "https://dl.google.com/android/repository/commandlinetools-linux-.*_latest.zip")

        DOWNLOAD_FILE_PATH=$ANDROID_HOME_CACHE/commandlinetools.zip
        wget -q --no-check-certificate -O "$DOWNLOAD_FILE_PATH" "$DOWNLOAD_LINK"
        
        if [ ! -f $DOWNLOAD_FILE_PATH ]; then
            echo "Failed to download Android command line tools!"
            exit 1
        fi

        if [ -d $ANDROID_HOME/cmdline-tools ]; then
            echo "Removing old Android command line tools ..."
            rm -rf $ANDROID_HOME/cmdline-tools
        fi

        unzip -qq -d $ANDROID_HOME_CACHE/ $DOWNLOAD_FILE_PATH
        rm -rf $DOWNLOAD_FILE_PATH
        mkdir -p $ANDROID_HOME/cmdline-tools
        mv $ANDROID_HOME_CACHE/cmdline-tools $ANDROID_HOME/cmdline-tools/latest

        echo "" >> /etc/profile
        echo "# Android command line tools" >> /etc/profile
        echo "export PATH=\$PATH:$ANDROID_HOME/cmdline-tools/latest/bin" >> $PATH_PROFILE
        . $PATH_PROFILE

        echo "Android command line tools installed!"
    fi
}

checkAndroidSdkPath() {
    echo "Checking Android sdk path ..."

    if [ ! -d $HOME/.android ]; then
        mkdir $HOME/.android
    fi

    if [ ! -f $HOME/.android/repositories.cfg ]; then
        touch $HOME/.android/repositories.cfg
    fi

    if [ ! -d $ANDROID_HOME ]; then
        mkdir -p $ANDROID_HOME
    fi

    if [ ! -d $ANDROID_HOME_CACHE ]; then
        mkdir -p $ANDROID_HOME_CACHE
    fi

    if [ "$ANDROID_SDK_ROOT" != "$ANDROID_HOME" ]; then
        echo "" >> /etc/profile
        echo "# Android SDK" >> /etc/profile
        echo "export ANDROID_SDK_ROOT=$ANDROID_HOME" >> $PATH_PROFILE
        . $PATH_PROFILE
    fi
}

checkAndroidSdk() {
    SDK_MANAGER=$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager 

    echo "Checking Android SDK Licenses ..."
    yes | $SDK_MANAGER --licenses  > /dev/null

    echo "Checking Android tools ..."
    $SDK_MANAGER "tools" > /dev/null

    echo "Checking Android platform-tools ..."
    $SDK_MANAGER "platform-tools" > /dev/null
}

checkLocalProperties() {
    echo "Checking project local.properties ..."

    if [ -f $PROJECT_PATH/local.properties ]; then
        echo "Removing old local.properties ..."
        rm -rf $PROJECT_PATH/local.properties
    fi
    
    echo "# This profile is generated by script." >> $PROJECT_PATH/local.properties
    echo "# Location of the SDK. This is only used by Gradle." >> $PROJECT_PATH/local.properties
    echo "# $(date)" >> $PROJECT_PATH/local.properties
    echo "sdk.dir=$ANDROID_HOME" >> $PROJECT_PATH/local.properties
}

initEnv
checkJDK
checkAndroidSdkPath
checkAndroidCmdLineTools
checkAndroidSdk
checkLocalProperties

echo "Setup complete!"
